<!--
@Create By Tim For 19/08/14
@单点登录成功回调组件
-->
<template>
  <div class="CodeCallbackContainer"></div>
</template>

<script>
import { parse } from 'qs'
import { setToken, getToken, setUser, getUser } from '@/utils/ssoUtils'
import { codeCallback, getInfo } from '@/api/user'

export default {
  name: 'CodeCallbackContainer',
  methods: {
    getUserInfo () {
      getInfo().then(res => {
        setUser(res.result)
        this.$store.dispatch('user/getInfo', res.result).then()
        this.$router.push({
            name: 'sso'
        })
      })
    },
    handleCallback ({ redirectUri, code }) {
      codeCallback( redirectUri, code ).then(res => {
        // 成功换取到token,将token保存到cookie
        setToken(res.result)
        this.$store.dispatch('user/login', res.result).then()
        //获取用户信息
        this.getUserInfo();
      })
    }
  },
  mounted () {
    const { redirect_uri, code } = parse(window.location.href.split('?')[1])
    this.handleCallback({ redirectUri: redirect_uri, code })
  }
}
</script>

<style scoped>
.AllCourseContainer {
  padding: 0 20px;
  min-width: 1440px;
  margin: 0 auto;
}
</style>